using System.Web;
using System.Web.Security;
using Ornament.MemberShip;
using Ornament.MemberShip.Permissions;
using StructureMap;
using StructureMap.Pipeline;
using System.Collections.Generic;
using System;

namespace Ornament.MVCWebFrame.Models
{
    /// <summary>
    /// </summary>
    public sealed class MemberShipContext
    {
        /// <summary>
        /// </summary>
        private static readonly MemberShipContext instance = new MemberShipContext();

        /// <summary>
        /// Prevents a default instance of the <see cref="MemberShipContext"/> class from being created. 
        /// </summary>
        private MemberShipContext()
        {
        }

        /// <summary>
        /// Gets CurrentContext.
        /// </summary>
        /// <value>
        /// The current context.
        /// </value>
        public static MemberShipContext CurrentContext
        {
            get { return instance; }
        }

        public bool CheckPermission(string resourceId, Enum operators)
        {
            IList<Permission> permissions;
            resourceId= resourceId.ToLower();
            if (HttpContext.Current.Session != null && HttpContext.Current.Session["Permissions"] == null)
            {                
                HttpContext.Current.Session["Permissions"] = this.CurrentUser.Permissions;            
            }
            else
            {
                HttpContext.Current.Session["Permissions"] = this.Factory.CreatePermissionDao().GetPermissions(HttpContext.Current.User.Identity.Name);
            }
            permissions = HttpContext.Current.Session["Permissions"] as IList<Permission>;
            foreach (var permission in permissions)
            {
                if (permission.ResourceInfoId.ToLower() == resourceId)
                {
                    if (permission.HasOperator(operators))
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        public User CurrentUser
        {
            get
            {
                User u = Factory.CreateUserDao().Get(HttpContext.Current.User.Identity.Name);
                return u;

            }
        }

        /// <summary>
        /// Gets Factory.
        /// </summary>
        /// 
        /// 
        /// <value>
        /// The factory.
        /// </value>
        public IMemberShipFactory Factory
        {
            get { return ObjectFactory.GetInstance<IMemberShipFactory>(); }
        }
        /// <summary>
        /// 
        /// </summary>
        public static ResourceFactory ResourceFactory
        {
            get
            {
                return ObjectFactory.GetInstance<ResourceFactory>();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void Initialize()
        {
            IRoleDao roleDao = CurrentContext.Factory.CreateRoleDao();
            Role role = roleDao.Get("admin");
            if (role == null)
            {
                role = new Role("admin");
                roleDao.SaveOrUpdate(role);
            }

            IUserDao userDao = CurrentContext.Factory.CreateUserDao();
            User user = userDao.Get("admin");
            bool needSave = false;
            if (user == null)
            {
                MembershipCreateStatus status;
                MembershipUser u = Membership.CreateUser("admin", "123456", "admin@admin.com", "question", "answer",
                                                         true, out status);
                needSave = true;
            }
            if (!user.Roles.Contains(role))
            {
                user.Roles.Add(role);
                needSave = true;
            }
            if (needSave)
                userDao.Save(user);
        }
    }
}